package com.gksc.video.device;

import com.gksc.video.data.DeviceInfo;
import com.gksc.video.data.OpResInfo;
import lombok.extern.slf4j.Slf4j;

import java.io.PipedOutputStream;

/**
 * @className: DHDevicePlay
 * @description: 大华设备播放取流实现类
 * @author: Liao.yx
 * @email: 1229053515@qq.com
 * @create: 2021年05月19日 15时55分
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 **/
@Slf4j
public class DHDevicePlay {
    /**
     * 登录句柄
     */
    private NetSDKLib.LLong lUserId = new NetSDKLib.LLong(0);
    /**
     * 播放句柄
     */
    private NetSDKLib.LLong lHandle = new NetSDKLib.LLong(0);
    /**
     * 管道输出流
     */
    private PipedOutputStream ops;
    /**
     * 帧率
     */
    private Integer bitRate;

    public DHDevicePlay(PipedOutputStream ops, Integer bitRate) {
        this.ops = ops;
        this.bitRate = bitRate;
    }

    /**
     * 设备登录
     *
     * @param ip   设备ip
     * @param port 设备端口
     * @param user 设备用户
     * @param pwd  设备密码
     */
    private void loginDevice(String ip, short port, String user, String pwd) {
        boolean initFlag = NetSDKLib.INSTANCE.CLIENT_Init(null, null);
        if (!initFlag) {
            NetSDKLib.INSTANCE.CLIENT_SetAutoReconnect(null, null);
            NetSDKLib.INSTANCE.CLIENT_SetConnectTime(5000, 1);
            NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
            netParam.nConnectTime = 10000;
            netParam.nGetConnInfoTime = 3000;
            NetSDKLib.INSTANCE.CLIENT_SetNetworkParam(netParam);
            NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
            pstInParam.nPort = port;
            pstInParam.szIP = ip.getBytes();
            pstInParam.szPassword = pwd.getBytes();
            pstInParam.szUserName = user.getBytes();
            NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
            NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
            pstOutParam.stuDeviceInfo = m_stDeviceInfo;
            lUserId = NetSDKLib.INSTANCE.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        } else {
            NetSDKLib.INSTANCE.CLIENT_Cleanup();
            log.error("大华sdk初始化失败!错误码{}", NetSDKLib.INSTANCE.CLIENT_GetLastError());
        }
    }


    /**
     * 退出登录
     */
    private void logoutDevice() {
        NetSDKLib.INSTANCE.CLIENT_Logout(lUserId);
        NetSDKLib.INSTANCE.CLIENT_Cleanup();
    }

    /**
     * 停止回放
     */
    public void stop() {
        if (lUserId.longValue() != 0) {
            logoutDevice();
        }
    }

    /**
     * 回放视频
     *
     * @param device
     * @return
     */
    public OpResInfo<Integer> play(DeviceInfo device) {
        loginDevice(device.getDevIp(), device.getDevPort(), device.getUsername(), device.getPassword());
        if (lUserId.intValue() == 0) {
            int errorCode = HCNetSDK.INSTANCE.NET_DVR_GetLastError();
            return new OpResInfo<>("大华sdk登录失败", errorCode, false);
        }

        log.info("设备device{}播放成功", device.toString());
        return new OpResInfo<>("", 0, true);
    }
}
